package com.muyu.danmo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.muyu.danmo.mapper.CommentsInfoMapper;
import com.muyu.danmo.mapper.GoodsEvaluationInfoMapper;
import com.muyu.danmo.mapper.ThumbsUpInfoMapper;
import com.muyu.danmo.service.EvaluationService;
import com.muyu.system.domain.CommentsInfo;
import com.muyu.system.domain.GoodsEvaluationInfo;
import com.muyu.system.domain.ThumbsUpInfo;
import com.muyu.system.domain.Vo.CommentListVO;
import com.muyu.system.domain.Vo.EvaluationCountVO;
import com.muyu.system.domain.Vo.EvaluationListVO;
import com.muyu.system.domain.dto.CommentListDTO;
import com.muyu.system.domain.dto.CommentsDTO;
import com.muyu.system.domain.dto.EvaluationListDTO;
import com.muyu.system.domain.dto.ThumbsUpDTO;
import com.muyu.system.domain.enums.ScoreTypeEnum;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
@RequiredArgsConstructor
public class EvaluationServiceImpl implements EvaluationService {

    private final GoodsEvaluationInfoMapper  goodsEvaluationInfoMapper;

    private final CommentsInfoMapper commentsInfoMapper;

    private final ThumbsUpInfoMapper thumbsUpInfoMapper;

    @Override
    public EvaluationCountVO count(Long goodId) {
        EvaluationCountVO evaluationCountVO = new EvaluationCountVO();
        Long imageCount = new LambdaQueryChainWrapper<>(goodsEvaluationInfoMapper).eq(GoodsEvaluationInfo::getGoodId, goodId)
                .isNotNull(GoodsEvaluationInfo::getImage).count();
        evaluationCountVO.setImageCount(imageCount.intValue());
        List<GoodsEvaluationInfo> list = new LambdaQueryChainWrapper<>(goodsEvaluationInfoMapper).eq(GoodsEvaluationInfo::getGoodId, goodId)
                .select(GoodsEvaluationInfo::getId, GoodsEvaluationInfo::getScoreType).list();
        evaluationCountVO.setSum(list.size());
        Map<ScoreTypeEnum, List<GoodsEvaluationInfo>> collect = list.stream().collect(Collectors.groupingBy(GoodsEvaluationInfo::getScoreType));
        evaluationCountVO.setGoodCount(collect.get(ScoreTypeEnum.GOOD) == null ? 0 : collect.get(ScoreTypeEnum.GOOD).size());
        evaluationCountVO.setBadCount(collect.get(ScoreTypeEnum.BAD) == null ? 0 : collect.get(ScoreTypeEnum.BAD).size());
        evaluationCountVO.setMiddleCount(collect.get(ScoreTypeEnum.MIDDLE) == null ? 0 : collect.get(ScoreTypeEnum.MIDDLE).size());
        return evaluationCountVO;
    }

    @Override
    public IPage<EvaluationListVO> list(EvaluationListDTO evaluationListDTO) {
        return goodsEvaluationInfoMapper.list(new Page<>(evaluationListDTO.getPageNum(), evaluationListDTO.getPageSize()),evaluationListDTO);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void postComment(CommentsDTO commentsDTO) {
        CommentsInfo commentsInfo = new CommentsInfo();
        BeanUtil.copyProperties(commentsDTO, commentsInfo);
        commentsInfoMapper.insert(commentsInfo);
        GoodsEvaluationInfo one = new LambdaQueryChainWrapper<>(goodsEvaluationInfoMapper).eq(GoodsEvaluationInfo::getId, commentsDTO.getEvaluationId()).one();
        one.setCommentsNumber(one.getCommentsNumber() + 1);
        goodsEvaluationInfoMapper.updateById(one);

    }

    @Override
    public IPage<CommentListVO> findCommentList(CommentListDTO commentListDTO) {
        return commentsInfoMapper.findCommentList(new Page<>(commentListDTO.getPageNum(), commentListDTO.getPageSize()), commentListDTO.getEvaluationId());
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void thumbsUp(ThumbsUpDTO thumbsUpDTO) {
        ThumbsUpInfo thumbsUpInfo = new LambdaQueryChainWrapper<>(thumbsUpInfoMapper).eq(ThumbsUpInfo::getEvaluationId, thumbsUpDTO.getEvaluationId())
                .eq(ThumbsUpInfo::getUserId, thumbsUpDTO.getUserId()).one();
        if(null == thumbsUpInfo){
            ThumbsUpInfo thumbsUp = new ThumbsUpInfo();
            BeanUtil.copyProperties(thumbsUpDTO, thumbsUp);
            thumbsUpInfoMapper.insert(thumbsUp);
        }else {
            thumbsUpInfo.setLikeStatus(thumbsUpDTO.getLikeStatus());
            thumbsUpInfoMapper.updateById(thumbsUpInfo);
        }
        if(null == thumbsUpInfo || !thumbsUpInfo.getLikeStatus().equals(thumbsUpDTO.getLikeStatus())){
            GoodsEvaluationInfo goodsEvaluationInfo = goodsEvaluationInfoMapper.selectById(thumbsUpDTO.getEvaluationId());
            if(thumbsUpDTO.getLikeStatus().equals(true)){
                goodsEvaluationInfo.setThumbsUp(goodsEvaluationInfo.getThumbsUp() + 1);
            }else {
                goodsEvaluationInfo.setThumbsUp(goodsEvaluationInfo.getThumbsUp() - 1);
            }
            goodsEvaluationInfoMapper.updateById(goodsEvaluationInfo);
        }
    }
}
